var xhr = new XMLHttpRequest();
xhr.open("GET", "php/index.php?hide=getbooks")
xhr.send(null);
xhr.onreadystatechange = () => {
    if (xhr.readyState == 4 && xhr.status == 200) {
        var data = JSON.parse(xhr.responseText);
        var table = document.createElement("table");
        var arr = ["序号", "书名", "作者", "单价", "数量", "简介", "操作"];
        var tr = document.createElement("tr");
        arr.forEach(str => {
            var td = document.createElement("td");
            td.innerHTML = str;
            tr.appendChild(td);
        })
        table.appendChild(tr);
        data.forEach(obj => {
            var tr = document.createElement("tr");
            for (var i in obj) {
                if (i == "content" || i == "vip") { continue };
                var td = document.createElement("td");
                td.innerHTML = obj[i];
                tr.appendChild(td);
            }
            var td = document.createElement("td");
            td.innerHTML = "<button style='background:skyblue' onclick='look(this)'>查看</button><button style='background:orange' onclick='modify(this)'>修改</button><button style='background:red' onclick='del(this)'>删除</button>";
            td.style.display = "flex";
            td.style.justifyContent = "space-between";
            tr.appendChild(td);
            table.appendChild(tr);
        })
        tabs.appendChild(table);
    }
}
ins.onclick = () => {
    location = "html/ins.html";
}
function look(btn) {
    var id = btn.parentNode.parentNode.firstElementChild.innerHTML;
    sessionStorage.id = id;
    location = "html/content.html";
}
function del(btn) {
    var id = btn.parentNode.parentNode.firstElementChild.innerHTML;
    var xhr = new XMLHttpRequest();
    xhr.open("GET", `php/index.php?hide=delbooks&id=${id}`);
    xhr.send(null);
    xhr.onreadystatechange = () => {
        if (xhr.readyState == 4 && xhr.status == 200) {
            if (xhr.responseText == 1) {
                btn.parentNode.parentNode.remove();
            }
        }
    }
}
function modify(btn) {
    var id = btn.parentNode.parentNode.firstElementChild.innerHTML;
    sessionStorage.id=id;
    location="html/modify.html";
}